﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AMS.DeDuplicator.BusinessLayer.Command;
using AMS.DeDuplicator.CommonLayer;
using AMS.DeDuplicator.CommonLayer.Adapter;

namespace AMS.DeDuplicator.ApplicationLayer.State
{
    public class QuickKeys
    {
        IState _currentState;
        List<IState> _validStates;
        TreeViewAdapter _tv;

        // how do I expose Command.Undo()?

        public QuickKeys(TreeViewAdapter tv)
        {
            InputValidator.ValidateNotNull(tv);
            _tv = tv;
        }

        private void BuildQuickKeys()
        {
            IState startState = new State();
            _validStates.Add(startState);
            IState ctrlState = new State(startState);
            _validStates.Add(ctrlState);
            Transition t = new Transition(ctrlState, null);
            startState.AddTransition(Keys.ControlKey, t);
            IState ctrlShiftState = new State(startState);
            _validStates.Add(ctrlShiftState);
            t = new Transition(ctrlShiftState, null);
            ctrlState.AddTransition(Keys.Shift, t);
            IState ctrlShiftKState = new State(startState);
            _validStates.Add(ctrlShiftKState);
            t = new Transition(ctrlShiftKState, null);
            ctrlShiftState.AddTransition(Keys.K, t);
            
            // Ctrl-Shift-K-D -> Delete All Duplicates (regardless of checked?)
            IState ctrlShiftKDState = new State(startState);
            _validStates.Add(ctrlShiftKDState);
            t = new Transition(startState, new DeleteAllDuplicatesCommand(_tv));
            ctrlShiftKDState.AddTransition(Keys.D, t);

            // TODO: Ctrl-Shift-K-R -> Replace All Duplicates
        }

        public void ProcessInput(Keys key)
        {
            IState nextState = _currentState.ProcessInput(key);
            if (_validStates.Contains(nextState))
                _currentState = nextState;
            else
                throw new Exception("invalid target state");
        }
    }
}
